//
//  WelcomeLayer.m
//  CutFruit
//
//  Created by BulletHermit on 13-6-19.
//  Copyright 2013年 __MyCompanyName__. All rights reserved.
//

#import "WelcomeLayer.h"
#import "GameLayer.h"
#import "AppDelegate.h"
#import <CoreData/CoreData.h>

@implementation WelcomeLayer
@synthesize playButton,lifeButton,lifeLabel,timeLabel,timeButton;
-(id)init
{
    if (self = [super init]) {
        msgLayer = [MessageLayer node];
        [self addChild:msgLayer z:10];
        msgLayer.visible=NO;
        [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"nature_bgm.aifc"];
        lifeButton.visible=NO;
        lifeLabel.visible=NO;
        timeButton.visible=NO;
        timeLabel.visible=NO;
        
    }
    return self;
}
-(void) play:(id)sender
{
    lifeButton.visible=YES;
    lifeLabel.visible=YES;
    timeButton.visible=YES;
    timeLabel.visible=YES;
    playButton.visible=NO;

}
-(void) playLifeMode:(id)sender
{
    CCScene* gameScene = [GameLayer sceneWithGameMode:kGameModeLife];
    
    // Go to the game scene
    [[CCDirector sharedDirector] replaceScene:gameScene];
}
-(void) playTimeMode:(id)sender
{
    CCScene* gameScene = [GameLayer sceneWithGameMode:kGameModeTime];
    
    // Go to the game scene
    [[CCDirector sharedDirector] replaceScene:gameScene];
}
-(void)setSound:(id)sender
{
    CCLOG(@"setSound");
    playButton.visible=NO;
    msgLayer.visible=YES;
}
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: location];
    CGSize screen = [CCDirector sharedDirector].winSize;
    CGRect bBox={ccpAdd([msgLayer.bg boundingBox].origin,ccp(screen.width*0.5,screen.height*0.5)),[msgLayer.bg boundingBox].size};
    if (!CGRectContainsPoint(bBox,location)) {
        
        playButton.visible=YES;
        msgLayer.visible=NO;
    }
    
}
-(void) onExit
{
    AppController *appDelegate =
    (AppController*)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription
                                              entityForName:@"Volume"
                                              inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];
    
    NSError *error;
    NSArray *objects = [context executeFetchRequest:request error:&error];
    if (objects == nil) {
        NSLog(@"There was an error!");
        // Do whatever error handling is appropriate
    }

    for (NSManagedObject *oneObject in objects) {
        NSString* kind = [oneObject valueForKey:@"kind"];
        if ( [kind compare:@"sound"]) {
            float ev = [[SimpleAudioEngine sharedEngine] effectsVolume];
            [oneObject setValue:[NSNumber numberWithFloat:ev ]  forKey:@"value"];
        }
        else if([kind compare:@"music"])
        {
            float ev = [[SimpleAudioEngine sharedEngine] backgroundMusicVolume];
            [oneObject setValue:[NSNumber numberWithFloat:ev ]  forKey:@"value"];
        }
            
        
    }
    [context save:&error];


}
@end
